package com.lagou.edu.ad.remote;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lagou.edu.ad.entity.PromotionAd;
import com.lagou.edu.ad.entity.PromotionSpace;
import com.lagou.edu.ad.service.IPromotionAdService;
import com.lagou.edu.ad.service.IPromotionSpaceService;
import com.lagou.edu.common.util.ConvertUtils;
import com.lagou.edu.dto.PromotionAdDTO;
import com.lagou.edu.dto.PromotionSpaceDTO;
import com.lagou.edu.remote.AdRemoteService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * AdRemoteServiceImpl
 *
 * @author zyt
 * @date 2021/2/20 20:09
 **/
@RestController
@RequestMapping("/ad")
public class AdRemoteServiceImpl implements AdRemoteService {
    
    @Autowired
    private IPromotionSpaceService promotionSpaceService;
    
    @Autowired
    private IPromotionAdService promotionAdService;
    
    @Override
    //@GetMapping("/space/getAllSpaces")
    public List<PromotionSpaceDTO> getAllSpaces() {
        List<PromotionSpace> spaceList = promotionSpaceService.list();
        
        return ConvertUtils.convertList(spaceList, PromotionSpaceDTO.class);
    }
    
    @Override
    public List<PromotionSpaceDTO> getAdBySpace(String[] spaceKey) {
        List<PromotionSpace> spaceList = promotionSpaceService.list(
                new QueryWrapper<PromotionSpace>()
                        .lambda()
                        .in(PromotionSpace::getSpaceKey, Arrays.asList(spaceKey)));
        List<PromotionSpaceDTO> dtoList = new ArrayList<>();
        spaceList.forEach(space -> {
            PromotionSpaceDTO promotionSpaceDTO = new PromotionSpaceDTO();
            ConvertUtils.convert(space, promotionSpaceDTO);
            List<PromotionAd> adList = promotionAdService.list(
                    new QueryWrapper<PromotionAd>()
                            .lambda()
                            .eq(PromotionAd::getSpaceId, space.getId()));
            List<PromotionAdDTO> promotionAdDTOS = ConvertUtils.convertList(adList, PromotionAdDTO.class);
            promotionSpaceDTO.setAdDTOList(promotionAdDTOS);
            dtoList.add(promotionSpaceDTO);
        });
        return dtoList;
    }
}
